//自行模拟实现一个线程池main
//只实现固定线程数目的线程池

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

class MyFixedThreadPool{
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    public MyFixedThreadPool(int n){
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
                while(true){
                    try {
                        //从队列中循环取出一个任务
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }    
    }
    public void submit(Runnable task) throws InterruptedException{
        //把任务添加到队列中
        queue.put(task);
    }

}
public class Demo34 {
public static void main(String[] args) throws InterruptedException {
       MyFixedThreadPool threadPool = new MyFixedThreadPool(2);
       for(int i=0;i<100;i++){
        int id=i;
        threadPool.submit(new Runnable(){
            @Override
            public void run() {
                System.out.println("执行一些任务"+id);
            }
        });
       }
}

}
